'''
Created on Aug 24, 2010

@author: alia
'''
import logging
log = logging.getLogger('hud')
log.debug("imported")
import time
import cocos
from cocos.text import Label
from cocos.actions import *
from bg_layer import BGLayer
from cocos.director import director
import game_audio

class Hud(cocos.layer.Layer):
    #is_event_handler = False
    def __init__(self, game):
        super(Hud, self).__init__()

        self.game = game
        self.game.push_handlers( self.on_game_over,  self.on_xp_gain,  self.on_game_win,  self.on_game_start , self.on_complete_level)
        
        #self.add(BGLayer("TEMP_HUD"))
        
        self.exp = Label("Exp: ", font_name='Times New Roman',
            font_size=16,
            x=100, y=500,
            anchor_x='center', anchor_y='center')

        self.add(self.exp)
        cocos.actions.FadeOut(0)
                
        self.timer = Label("Time: 0.0 s", font_name='Times New Roman',
            font_size=16,
            x=700, y=500)

        self.add(self.timer)
        self.start_time = time.time()
        self.clock = time.time()
        self.schedule(self.update_time)
        
    def update_time(self, dt=0):
        self.clock = time.time()
        self.timer.element.text = "Time: %0.1f s" % (self.clock-self.start_time)

    def on_complete_level(self):
        self.on_good_condition("Level Complete")
        
    def on_game_over(self):
        x,y = director.get_window_size()
        loose = Label("You Loose", font_name='Times New Roman',
            font_size=32,
            anchor_x='center', anchor_y='center')
        self.add(loose)
        loose.do(MoveTo((x/3,y/2), 3) | ScaleBy( 2, 3 ) )
    
    def on_xp_gain(self):
        log.info("on_xp_gain")
        self.exp.element.text = "Exp: %d" % self.game.player.exp
            
    def on_game_start(self):
        self.do(FadeIn(2))
        self.on_xp_gain()
        self.start_time = time.time()
        self.clock = self.start_time
        
    def on_game_win(self):
        self.on_good_condition("You Winnar")
        
    def on_good_condition(self, message):
        self.unschedule(self.update_time)
        game_audio.play_song('music_victory.ogg')
        x,y = director.get_window_size()
        self.timer.do(MoveTo((x/3,y/2), 3)  )
        loose = Label(message, font_name='Times New Roman',
            font_size=32,
            anchor_x='center', anchor_y='center')
        self.add(loose)
        loose.do(MoveTo((x/3,y/3), 3) )
